<?php
function afficherVideo($row){
    connect();
    $requete = "UPDATE `Video` SET `nb_vu`=`nb_vu`+1 WHERE `id` = '{$row['id']}'";
    $reponse = executerRequete($requete, $GLOBALS['debug']);

    if (false && ereg("\.mp4$", $row['lien']))
    echo <<<END

<video width="640" height="360" poster="appercu{$row['id']}.JPG" controls>
            <source src="{$row['lien']}" type="video/mp4"></source>
            <source src="{$row['lien']}.ogg" type="video/ogg"></source><!--[if gt IE 6]>
            <object width="640" height="375" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"><!
            [endif]--><!--[if !IE]><!-->
            <object width="640"  height="375" type="video/quicktime" data="video/{$row['lien']}">
            <!--<![endif]-->
            <param name="src" value="video/{$row['lien']}" />
            <param name="showlogo" value="false" />
            <param name="autoplay" value="false" />
            <object width="640" height="384" type="application/x-shockwave-flash"
                    data="player.swf?image=appercu{$row['id']}.JPG&amp;file=video/{$row['lien']}">
                    <param name="movie" value="video/player.swf?image=appercu{$row['id']}.JPG&amp;file=video/{$row['lien']}" />
                    <img src="appercu{$row['id']}.JPG" width="640" height="360" alt="{$row['titre']}"
                         title="No video playback capabilities, please download the video below" />
            </object><!--[if gt IE 6]><!--></object><!--<![endif]-->
        </video>

END;
    else
echo <<<END
        <img class="grandApercu" src="apercus/apercu{$row['id']}.jpg" alt="Prévisualisation de la vidéo">
END;

    $requete = "SELECT AVG(note) as moyenne FROM `Note` WHERE id_video={$row['id']}";
    $reponse = executerRequete($requete, $GLOBALS['debug']);
    $donnees = mysql_fetch_array($reponse);
    echo "<a href=\"{$row['lien']}\">Télécharger</a><br/>";

    // Affichage de la note moyenne
    $noteIconesAJAX = round($donnees['moyenne']) ;
    echo "Note moyenne : ";
    echo "<div class=\"noteMoyenne\">" ;
    require("ajax/noteVideoIcones.php") ;
    echo "</div>".PHP_EOL ;
    
    
    // Vote
    if (connecte())
    {
        $requete = "SELECT * FROM Note WHERE id_video='{$row['id']}' AND pseudo='".pseudo()."';" ;
        $result = executerRequete($requete, $GLOBALS['debug']) ;
        $nombreLignes = mysql_num_rows($result) ;
?>

    <div class="notevideo">
    Votre note :
    <div class="imagesVote">
    <?php
    // Recherche de l'éventuelle note déjà donnée pour l'affichage
    $result = executerRequete ("SELECT note FROM Note WHERE pseudo='".pseudo()."' AND id_video='".$row['id']."';") ;
    if (mysql_num_rows($result) > 0)
    {
        $d = mysql_fetch_assoc($result) ;
        $maNote = $d['note'] ;
    }
    else $maNote = 0 ;
    for ($i = 1 ; $i<=5 ; $i++)
    {
        if ($i <= $maNote) $nomImage = "StarOver.png" ;
        else $nomImage = "StarOut.png" ;
        echo '<img class="vote" id="Star'.$i.'" src="video/'.$nomImage.'" />' ;
    }
    ?>
    </div>
    </div>
    <script language="JavaScript" type="text/JavaScript">

NbStar = 5;
var maNote = <?= $maNote ?> ;
StarBaseId='Star';
StarOutUrl="video/StarOut.png";
StarOverUrl="video/StarOver.png"
LgtStarBaseId=StarBaseId.lastIndexOf('');

function afficherNote(note)
{
    for (i=1;i<NbStar+1;i++)
    {
        if (i<=note)
            $("img#Star"+i).attr("src", StarOverUrl) ;
        else
            $("img#Star"+i).attr("src", StarOutUrl) ;
    }
}

for (i=1;i<NbStar+1;i++) {
    var img=$('img#Star'+i);

    img.click(function() {
        maNote = Name2Nb(this.id) ;
        alert('Vous avez donné la note de '+maNote+'.');
        //$("img.vote").remove();
        $.post("video/note.php", {"id_video" : <?= $row['id']?>, "note" : Name2Nb(this.id)}, function(d){
            executerTache("Video", "update", <?= $row['id'] ?>) ;
        }) ;
        // On actualise la note moyenne
        
        

    });
    
    //Réaction lors du clic sur une étoile
    //Evidemment, il faudrait compléter cette fonction pour la rendre vraiment utile.
    //Par exemple, envoyer la note dans une base de donnée via un XMLHttpRequest.

    img.attr("alt", 'Donner la note de '+i);
    //Texte au survol

    //
    img.mouseover(function() {
        nouvelleNote = Name2Nb($(this).attr("id")) ;
        afficherNote(nouvelleNote);
    });
}
$("div.imagesVote").mouseout(function() {afficherNote(maNote);});
afficherNote(maNote) ;


// Scripts d'actualisation
ajouterTache("Video", "update", function(id){
    $.getJSON("utilities/jsonDescriptions/jsonDescriptionVideo.php?id="+id, {}, function(data){
        $("div.noteMoyenne").fadeOut("def", function(){
            $(this).load("ajax/noteVideoIcones.php?noteIconesAJAX="+data.note_moyenne).hide().fadeIn("def") ;
        }) ;
    }) ;
}) ;
</script>
<?php
    }

    mysql_close();
}

function videoSimilaire($id){
    connect();
    $requete = "SELECT Tag.* FROM `Tag` INNER JOIN `LienTagVideo` WHERE Tag.id = LienTagVideo.id_tag AND LienTagVideo.id_video={$id} ORDER BY RAND();";
    $reponse = executerRequete($requete, $GLOBALS['debug']);
    $row = mysql_fetch_array($reponse);
    $recherche = $row['tag'];
    while ($row = mysql_fetch_array($reponse)) {
        $recherche .= " ".$row['tag'];
    }

    echo "<div class='videoSimilaire'>";
    echo "<h2>Vidéos similaires</h2>";
    $tab = explode(' ', $recherche);
    rechercherVideo($tab, 5);
    echo "</div>";
}

function rechercherVideo($recherche, $limit=-1){
    $dim = count($recherche);
    connect();
    for($i = 0; $i < $dim; $i++){
        $requete = "SELECT * FROM `Video` WHERE `titre` like '%$recherche[$i]%';";
        $reponse = executerRequete($requete, $GLOBALS['debug']);

        while($donnees = mysql_fetch_array($reponse)){
            $pertinance[$donnees['id']] = $pertinance[$donnees['id']] + 1;
        }



        $requete_tag = "SELECT * FROM `Tag` WHERE `tag` like '%$recherche[$i]%'";
        $reponse = executerRequete($requete_tag, $GLOBALS['debug']);
        while($donnees = mysql_fetch_array($reponse)){
            $requete2 = "SELECT * FROM `LienTagVideo` WHERE `id_tag`={$donnees['id']}";
            $reponse2 = executerRequete($requete2, $GLOBALS['debug']);
            while($donnees2 = mysql_fetch_array($reponse2)){
                $requete3 = "SELECT * FROM `Video` WHERE `id`={$donnees2['id_video']}";
                $reponse3 = executerRequete($requete3, $GLOBALS['debug']);
                while($donnees3 = mysql_fetch_array($reponse3)){
                    $pertinance[$donnees3['id']] = $pertinance[$donnees3['id']] + 1;
                }
            }
        }

    }
    arsort($pertinance);
    $i = $limit ;
    foreach ($pertinance as $key => $value) {
        if ($i == 0) break ;
        afficherRecherche($key);
        $i-- ;
    }


    
    mysql_close();
}



function afficherRecherche($id_video){

    connect();
    $requete = "SELECT * FROM `Video` WHERE `id` = {$id_video}";
    $reponse = executerRequete($requete, $GLOBALS['debug']);
    $row = mysql_fetch_array($reponse);
    mysql_close();

    iconeVideo($row) ;
}

function iconeVideo($r)
{
    $row = $r ;
    require("ajax/iconeVideo.php") ;
}

function populaireVideo(){
    connect();
    $requete = "SELECT * FROM `Video` ORDER BY `note_moyenne` DESC LIMIT 0,5 ;";
    $reponse = executerRequete($requete, $GLOBALS['debug']);
    while($donnees = mysql_fetch_array($reponse)){
        iconeVideo($donnees);
    }
    //mysql_close();
}

?>
